﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using DalEx;

public partial class Signning : System.Web.UI.PageEx
{
    private DataSet ds = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataBinds();
            //loadHouseNature_rbt();
        }
    }
    private void DataBinds()
    {
        string strResult = string.Empty;
        ds = DalEx.DbCompInfoEx.Select(" and cStatus=1");
        rptType.DataSource = ds;
        rptType.DataBind();

        pnlLoginInfo.Attributes.Add("style", "min-height:80px");
        Model.DbCompInfo dbCompInfo = DalEx.DbCompInfoEx.GetModel("BaoMing");
        try
        {
            if (base.Request.QueryString["F"].ToString() == "1")
            {
                dbCompInfo = DalEx.DbCompInfoEx.GetModel("BaoMing1");
            }
            if (base.Request.QueryString["F"].ToString() == "2")
            {
                dbCompInfo = DalEx.DbCompInfoEx.GetModel("BaoMing2");
            }
            if (base.Request.QueryString["F"].ToString() == "3")
            {
                dbCompInfo = DalEx.DbCompInfoEx.GetModel("BaoMing3");
            }
        }
        catch
        {
        }
        
        if (dbCompInfo != null && (dbCompInfo.CEndDate.CompareTo(DateTime.Now) >= 0 || dbCompInfo.CEndDate.CompareTo(SqlHelper.MinDate)==0))
        {
            setMeta("学生入园报名表", dbCompInfo.CKeywords, dbCompInfo.CDescription);
        }
        else
        {
            MessageBox("抱歉，当前报名信息不存在或已结束报名！", "Default.aspx");
            return;
        }
        #region 绑定类型
        rblClass.Items.Clear();
        rblClass.Items.Add(new ListItem("小班", "0"));
        //rblClass.Items.Add(new ListItem("中班", "1"));
        //rblClass.Items.Add(new ListItem("大班", "2"));
        //rblClass.Items.Add(new ListItem("大大班", "3"));

        rblCensusClass.Items.Clear();
        //rblCensusClass.Items.Add(new ListItem("龙岗", "0"));
        //rblCensusClass.Items.Add(new ListItem("深圳其他区", "1"));
        rblCensusClass.Items.Add(new ListItem("深户", "1"));
        rblCensusClass.Items.Add(new ListItem("非深户", "2"));
        rblCensusClass.Items.Add(new ListItem("港籍", "3"));
        rblCensusClass.Items.Add(new ListItem("台籍", "4"));
        rblCensusClass.Items.Add(new ListItem("澳籍", "5"));
        //rblCensusClass.Items.Add(new ListItem("外籍", "6"));

        rbtHouseNature.Items.Clear();
        //rbtHouseNature.Items.Add(new ListItem("自建房", "0"));
        rbtHouseNature.Items.Add(new ListItem("购房", "1"));
        //rbtHouseNature.Items.Add(new ListItem("租房", "2"));
        //rbtHouseNature.Items.Add(new ListItem("单位房", "3"));
        //rbtHouseNature.Items.Add(new ListItem("亲友房", "4"));
        rbtHouseNature.Items.Add(new ListItem("其他", "5"));

        rbtPlanning.Items.Clear();
        rbtPlanning.Items.Add(new ListItem("独生子女", "0"));
        //rbtPlanning.Items.Add(new ListItem("非独生子女", "1"));
        //rbtPlanning.Items.Add(new ListItem("超生但办理罚款手续", "2"));
        //rbtPlanning.Items.Add(new ListItem("超生未办理罚款手续", "3"));
        rbtPlanning.Items.Add(new ListItem("政策内二孩", "4"));
        rbtPlanning.Items.Add(new ListItem("政策外但已接受处理的幼儿", "5"));

        //rbtCaseType.Items.Clear();
        //rbtCaseType.Items.Add(new ListItem("住得近", "0"));
        //rbtCaseType.Items.Add(new ListItem("收费合理", "1"));
        //rbtCaseType.Items.Add(new ListItem("办学质量好", "2"));
        //rbtCaseType.Items.Add(new ListItem("环境好", "3"));
        //rbtCaseType.Items.Add(new ListItem("师资好", "4"));
        //rbtCaseType.Items.Add(new ListItem("家长口碑好", "5"));
        //rbtCaseType.Items.Add(new ListItem("社会口碑好", "6"));
        //rbtCaseType.Items.Add(new ListItem("别无选择", "7"));

        rbtNurserySchool.Items.Clear();
        rbtNurserySchool.Items.Add(new ListItem("龙岗区机关幼儿园本部", "0"));
        rbtNurserySchool.SelectedValue = "0";
        try
        {
            if (base.Request.QueryString["F"].ToString() == "1")
            {
                rbtNurserySchool.Items.Clear();
                rbtNurserySchool.Items.Add(new ListItem("龙岗区机关幼儿园龙城分园", "1"));
                rbtNurserySchool.SelectedValue = "1";
            }
            if (base.Request.QueryString["F"].ToString() == "2")
            {
                rbtNurserySchool.Items.Clear();
                rbtNurserySchool.Items.Add(new ListItem("龙岗区机关幼儿园南湾分园", "2"));
                rbtNurserySchool.SelectedValue = "2";
                rblClass.Items.Add(new ListItem("中班", "1"));
                rblClass.Items.Add(new ListItem("大班", "2"));
            }
            if (base.Request.QueryString["F"].ToString() == "3")
            {
                rbtNurserySchool.Items.Clear();
                rbtNurserySchool.Items.Add(new ListItem("龙岗区机关幼儿园坂田分园", "3"));
                rbtNurserySchool.SelectedValue = "3";
                //rblClass.Items.Add(new ListItem("大班", "2"));
            }
        }
        catch
        {
        }
        //rbtNurserySchool.Items.Add(new ListItem("龙岗区机关幼儿园龙城分园", "1"));
        //rbtNurserySchool.Items.Add(new ListItem("龙岗区机关幼儿园南湾分园", "2"));
        //rbtNurserySchool.Items.Add(new ListItem("龙岗区机关幼儿园坂田分园", "3"));
        #endregion
        
        #region 如会员已登录则绑定数据
        if (DalEx.DbUserDalEx.CheckUserLogin(UserType, UserId, ref strResult))
        {
            pnlLoginInfo.Enabled = false;
            pnlLoginInfo.Visible = false;

            Model.DbUsers dbUsers = DalEx.DbUserDalEx.Select(UserId);
            if (dbUsers != null)
            {
                switch (dbUsers.Sex)
                { 
                    case 0:
                        txtMotherName.Text = dbUsers.FirstName + dbUsers.LastName;
                        txtMotherTel.Text = dbUsers.Tel;
                        break;
                    case 1:
                        txtFatherName.Text = dbUsers.FirstName + dbUsers.LastName;
                        txtFatherTel.Text = dbUsers.Tel;
                        break;
                }
                hfUserId.Value = dbUsers.UserID.ToString();
                txtHouseAddr.Text = dbUsers.Address;
            }
        }
        #endregion
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //
        Model.DbCompInfo dbCompInfo = DalEx.DbCompInfoEx.GetModel("BaoMing");
        if (dbCompInfo == null || (dbCompInfo.CEndDate.CompareTo(DateTime.Now) < 0 && dbCompInfo.CEndDate.CompareTo(SqlHelper.MinDate) != 0))
        {
            MessageBox("抱歉，当前报名信息不存在或已结束报名！", "Default.aspx");
            return;
        }
        int iResult = 0;
        string strResult = string.Empty;
        short sSex = 0, sRegClass = 0, sCensusClass = 0, sIfInSchool = 0, sFatherInSZ = 0, sFatherPaySB = 0, sFatherPayJZZ = 0;
        short sMotherInSZ = 0, sMotherPaySB = 0, sMotherPayJZZ = 0, sHouseNature = 0, sPlanning = 0;
        short sHouseRegion = 0, sHouseGroup = 0, sHouseNumber = 0, sHouseNumber1 = 0, sHouseNumber2 = 0;
        //int iChildCounts = 0, iChildIndex = 0;
        DateTime dtBirthday = SqlHelper.MinDate, dtBuyHouseDate = SqlHelper.MinDate, dtRentHouseDate = SqlHelper.MinDate, dtSignDate = SqlHelper.MinDate;
        string strOrderSN = WebBase.Comm.GetOrderNo("ZC");
        string strUserName = SqlHelper.String(txtUserName.Text);
        short.TryParse(rblSex.SelectedValue, out sSex);
        if (!string.IsNullOrEmpty(calBirthday.Text))
            DateTime.TryParse(calBirthday.Text, out dtBirthday);
        string strPlaceOrigin = SqlHelper.String(txtPlaceOrigin.Text);
        string strPlaceBirth = SqlHelper.String(txtPlaceBirth.Text);
        short.TryParse(rblClass.SelectedValue, out sRegClass);
        string strIdCardNo = SqlHelper.String(txtIdCardNo.Text);
        short.TryParse(rblCensusClass.SelectedValue, out sCensusClass);
        string strCensusAddr = SqlHelper.String(txtCensusProvince.Text) + "省" + SqlHelper.String(txtCensusCity.Text) + "市" + SqlHelper.String(txtCensusArea.Text) + "区（县）" + SqlHelper.String(txtCensusPoliceStation.Text)+"派出所";
        short.TryParse(rbtIsInSchool.SelectedValue, out sIfInSchool);
        string strHadInSchool = SqlHelper.String(txtHadInSchool.Text);
        string strAllergicHistory = SqlHelper.String(txtAllergicHistory.Text);
        string strSepcialDisease = SqlHelper.String(txtSepcialDisease.Text);
        string strFatherName = SqlHelper.String(txtFatherName.Text);
        string strFatherWorks = SqlHelper.String(txtFatherWorks.Text);
        string strFatherTel = SqlHelper.String(txtFatherTel.Text);
        short.TryParse(rblFatherInSZ.SelectedValue, out sFatherInSZ);
        short.TryParse(rblFatherPaySB.SelectedValue, out sFatherPaySB);
        short.TryParse(rblFatherPayJZZ.SelectedValue, out sFatherPayJZZ);
        short.TryParse(rbtPlanning.SelectedValue, out sPlanning);
        string strMotherName = SqlHelper.String(txtMotherName.Text);
        string strMotherWorks = SqlHelper.String(txtMotherWorks.Text);
        string strMotherTel = SqlHelper.String(txtMotherTel.Text);
        short.TryParse(rblMotherInSZ.SelectedValue, out sMotherInSZ);
        short.TryParse(rblMotherPaySB.SelectedValue, out sMotherPaySB);
        short.TryParse(rblMotherPayJZZ.SelectedValue, out sMotherPayJZZ);
        short.TryParse(rbtHouseNature.SelectedValue, out sHouseNature);
        //short sHouseRegion = 0, sHouseGroup = 0, sHouseNumber = 0;

        string strNoteNO = SqlHelper.String(TxtNoteNo.Text);
        string censusProvince = SqlHelper.String(txtCensusProvince.Text);
	    string censusCity = SqlHelper.String(txtCensusCity.Text) ;
	    string censusArea = SqlHelper.String(txtCensusArea.Text);
        string censusPoliceStation = SqlHelper.String(txtCensusPoliceStation.Text);
        string strHouseAddr = "";
        
         if (rbtHouseNature.SelectedValue == "1")
         {
             if (string.IsNullOrEmpty(calBuyHouseDate.Text))
             {
                 MessageBox("请选择购房时间！");
                 return;
             }
             if (string.IsNullOrEmpty(TxtNoteNo.Text))
             {
                 MessageBox("请填写不动产登记号！");
                 return;
             }
             short.TryParse(hfHouseRegionID.Value, out sHouseRegion);            
             short.TryParse(hfHouseGroupID.Value, out sHouseGroup);            
             short.TryParse(hfHouseNumberID.Value, out sHouseNumber);
             short.TryParse(hfHouseNumberID1.Value, out sHouseNumber1);
             short.TryParse(hfHouseNumberID2.Value, out sHouseNumber2);
                
             if (hfHouseRegionID.Value == "other")
             {
                 if (string.IsNullOrEmpty(txtHouseAddr.Text))
                 {
                     MessageBox("请填写家庭住址！");
                     return;
                 }
                 strHouseAddr += SqlHelper.String(txtHouseAddr.Text);
             }
             else
             {
                 if (sHouseRegion == 0)
                 {
                     MessageBox("请正确选择住房地址."); return;
                 }
                 if (sHouseGroup == 0)
                 {
                     MessageBox("请正确选择住房地址.."); return;
                 }
                 if (hfHouseNumber.Value.Equals("请选择") && sHouseNumber == 0)
                 {
                     MessageBox("请正确选择住房地址..."); return;
                 }
                 if (hfHouseNumber1.Value.Equals("请选择") && sHouseNumber1 == 0)
                 {
                     MessageBox("请正确选择住房地址...."); return;
                 }
                 if (hfHouseNumber2.Value.Equals("请选择") && sHouseNumber2 == 0)
                 {
                     MessageBox("请正确选择住房地址....."); return;
                 }
                 strHouseAddr = "深圳市龙岗区";
                 strHouseAddr += SqlHelper.String(hfHouseRegion.Value);
                 strHouseAddr += SqlHelper.String(hfHouseGroup.Value);
                 strHouseAddr += SqlHelper.String(hfHouseNumber.Value);
                 if (hfHouseNumber1.Value != "")
                 {
                     strHouseAddr += SqlHelper.String(hfHouseNumber1.Value);
                 }
                 if (hfHouseNumber2.Value != "")
                 {
                     strHouseAddr += SqlHelper.String(hfHouseNumber2.Value);
                 }
             }
         }else {
             if (string.IsNullOrEmpty(txtHouseAddr.Text))
             {
                 MessageBox("请填写家庭住址！");
                 return;
             }
             
             strHouseAddr = SqlHelper.String(txtHouseAddr.Text);
        }
        if (!string.IsNullOrEmpty(calBuyHouseDate.Text))
            DateTime.TryParse(calBuyHouseDate.Text, out dtBuyHouseDate);
        //int.TryParse(txtChildCounts.Text, out iChildCounts);
        //int.TryParse(txtChildIndex.Text, out iChildIndex);
        string strCaseType = string.Empty;
        string strNotes = SqlHelper.String(txtNotes.Text);
        string strNoteNo = SqlHelper.String(TxtNoteNo.Text);
        string strSignName = SqlHelper.String(txtSignName.Text);

        string strLoginName = SqlHelper.String(txtLoginUser.Text);
        string strEmail = SqlHelper.String(txtEmail.Text);
        string strLoginPwd = SqlHelper.String(txtLoginPwd.Text);

        dtSignDate = DateTime.Now;
        int iUserId = -1;
        int.TryParse(hfUserId.Value, out iUserId);
        iUserId = iUserId == 0 ? -1 : iUserId;

        short intNurserySchool = 0;
        short.TryParse(this.rbtNurserySchool.SelectedValue, out intNurserySchool);
        string strFatherCardNo = SqlHelper.String(txtFatherCardNo.Text);
        string strMotherCardNo = SqlHelper.String(txtMotherCardNo.Text);

        if (string.IsNullOrEmpty(strFatherName) && string.IsNullOrEmpty(strMotherName))
        {
            MessageBox("父母亲至少要填写一项！");
            return;
        }
        else
        {
            if (iUserId < 1)
            {
                Model.DbUsers dbUsers = DalEx.DbUserDalEx.GetModel(" and UserName='" + strLoginName + "' and UserPwd='" + SqlHelper.MD5(strLoginPwd, 32) + "'");
                if (dbUsers != null)
                {
                    iUserId = dbUsers.UserID;
                    strLoginName = dbUsers.UserName;
                }
                else
                {
                    dbUsers = new Model.DbUsers();
                    dbUsers.UserName = strLoginName;
                    dbUsers.Email = strEmail;
                    dbUsers.UserPwd = strLoginPwd;
                    dbUsers.Sex = 1;
                    dbUsers.AddTime = DateTime.Now;
                    dbUsers.LastLoginTime = SqlHelper.MinDate;
                    dbUsers.TotalCost = 0;
                    dbUsers.LoginTimes = 0;
                    dbUsers.Status = 1;
                    dbUsers.LastIP = "127.0.0.1";
                    if (DalEx.DbUserDalEx.Insert(dbUsers, ref strResult, ref iUserId) < 1)
                    {
                        MessageBox(strResult);
                        return;
                    }
                }
            }
            //foreach (ListItem lItem in rbtCaseType.Items)
            //{
            //    if (lItem.Selected)
            //        strCaseType += lItem.Value + ",";
            //}
            //if (strCaseType.EndsWith(","))
            //    strCaseType = strCaseType.TrimEnd(',');
            Model.BaoMing baoMing = new Model.BaoMing();
            baoMing.OrderNO = strOrderSN;
            baoMing.UserName = strUserName;
            baoMing.Sex = sSex;
            baoMing.Birthday = dtBirthday;
            baoMing.PlaceOrigin = strPlaceOrigin;
            baoMing.PlaceBirth = strPlaceBirth;
            baoMing.RegClass = sRegClass;
            baoMing.IdCardNo = strIdCardNo;
            baoMing.CensusClass = sCensusClass;
            baoMing.CensusAddr = strCensusAddr;
            baoMing.IfInSchool = sIfInSchool;
            baoMing.InSchoolInfo = strHadInSchool;
            baoMing.AllergicHistory = strAllergicHistory;
            baoMing.SepcialDisease = strSepcialDisease;
            baoMing.FatherName = strFatherName;
            baoMing.FatherWorks = strFatherWorks;
            baoMing.FatherTel = strFatherTel;
            baoMing.FatherInSZ = sFatherInSZ;
            baoMing.FatherPaySB = sFatherPaySB;
            baoMing.FatherPayJZZ = sFatherPayJZZ;
            baoMing.MotherName = strMotherName;
            baoMing.MotherWorks = strMotherWorks;
            baoMing.MotherTel = strMotherTel;
            baoMing.MotherInSZ = sMotherInSZ;
            baoMing.MotherPaySB = sMotherPaySB;
            baoMing.MotherPayJZZ = sMotherPayJZZ;
            baoMing.HouseAddr = strHouseAddr;
            baoMing.HouseNature = sHouseNature;
            baoMing.HouseRegionID = sHouseRegion;
            baoMing.HouseGroupID = sHouseGroup;
            baoMing.HouseNumberID = sHouseNumber;
            baoMing.HouseNumber1 = sHouseNumber1;
            baoMing.HouseNumber2 = sHouseNumber2;
            baoMing.NoteNo = strNoteNo;
            baoMing.BuyHouseDate = dtBuyHouseDate;
            baoMing.RentHouseDate = dtRentHouseDate;
            baoMing.Planning = sPlanning;
            //baoMing.ChildCounts = iChildCounts;
            //baoMing.ChildIndex = iChildIndex;
            baoMing.CaseType = strCaseType;
            baoMing.SignName = strSignName;
            baoMing.SignDate = dtSignDate;
            baoMing.Notes = strNotes;
            baoMing.AddTime = DateTime.Now;
            baoMing.Photos = string.Empty;
            baoMing.Status = 0;
            baoMing.UserId = iUserId;
            baoMing.NurserySchool = intNurserySchool;
            baoMing.FatherCardNo = strFatherCardNo;
            baoMing.MotherCardNo = strMotherCardNo;
            baoMing.CensusProvince = censusProvince;
            baoMing.CensusCity = censusCity;
            baoMing.CensusArea = censusArea;
            baoMing.CensusPoliceStation = censusPoliceStation;
            //
            iResult = DalEx.BaoMingEx.Insert(baoMing, ref strResult);
            if (iResult > 0)
            {
                UserType = 1;
                UserId = iUserId;
                UserName = strLoginName;
                MessageBox(strResult, "MyBaoMing.aspx");
            }
            else
            {
                MessageBox(strResult);
            }
        }

    }
    private void setMeta(string title, string key, string des)
    {
        string strWebTitle = string.Empty, strWebKeywords = string.Empty, strWebDescription = string.Empty;
        Model.DbSystem dbSystem = Dal.DbSystemDal.Select(1);
        if (dbSystem != null)
        {
            strWebTitle = dbSystem.STitle;
            strWebKeywords = dbSystem.SKeywords;
            strWebDescription = dbSystem.SDescription;
        }
        HtmlMeta keywords = new HtmlMeta(); // keywords
        HtmlMeta description = new HtmlMeta(); // description
        keywords.Name = "keywords";
        keywords.Content = string.IsNullOrEmpty(key) ? strWebKeywords : key;

        this.Title = string.IsNullOrEmpty(title) ? strWebTitle : title;

        description.Name = "description";
        description.Content = string.IsNullOrEmpty(des) ? strWebDescription : des;

        Page.Header.Controls.Add(keywords);
        Page.Header.Controls.Add(description);
    }

    public void rbtHouseNature__SelectedIndexChanged(object sender, EventArgs e)
    {
        string strSelectHouseNature = rbtHouseNature.SelectedValue;
        if (strSelectHouseNature == "1")
        {
            loadHouseNature_rbt();
        }
        else
        {
            setVisibleTxt(true);
        }
    }

    private void loadHouseNature_rbt()
    {
        if (rbtHouseNature.SelectedValue == "1") { 
            setVisibleTxt(false);
            rbtHouseRegion.DataSource = DbHouseRegionEx.Select();
            rbtHouseRegion.DataTextField = "HouseRegionName";
            rbtHouseRegion.DataValueField = "HouseRegionID";
            rbtHouseRegion.DataBind();
        }
        else
        {
            setVisibleTxt(true);
        }

    }

    public void houseRegion__SelectedIndexChanged(object sender, EventArgs e)
    {
        string strSelectHouseRegion = rbtHouseRegion.SelectedValue;
        rbtHouseGroup.DataSource = DbHouseGroupEx.Select(" and houseRegionID=" + strSelectHouseRegion);
        rbtHouseGroup.DataTextField = "HouseGroupName";
        rbtHouseGroup.DataValueField = "HouseGroupID";
        rbtHouseGroup.DataBind();
    }

    public void houseGroup__SelectedIndexChanged(object sender, EventArgs e)
    {
        string strSelectHouseGroup = rbtHouseGroup.SelectedValue;
        rbtHouseNumber.DataSource = DbHouseNumberEx.Select(" and houseGroupID=" + strSelectHouseGroup);
        rbtHouseNumber.DataTextField = "HouseNumberName";
        rbtHouseNumber.DataValueField = "HouseNumberID";
        rbtHouseNumber.DataBind();
    }

    private void setVisibleTxt(bool isVisible)
    {
        if (isVisible)
        {
            txtHouseAddr.Visible = true;
            rbtHouseGroup.Visible = false;
            rbtHouseRegion.Visible = false;
            rbtHouseNumber.Visible = false;
        }
        else {
            txtHouseAddr.Visible = false;
            rbtHouseGroup.Visible = true;
            rbtHouseRegion.Visible = true;
            rbtHouseNumber.Visible = true;
        }
    }

}